import { ref } from 'vue'
import { defineStore } from 'pinia'
import {
  getPersonalWorkListAPI,
  getWorkTEmplateListAPI,
} from '@/request/modules/work'
import type { TemplateItem } from '@/views/templateList/type'
import type { IGetWorkTemplateListProps } from '@/request/types/workType'
import type { IWorkTemplateList } from '@/components/workTemplateList/work-template-list.vue'
const useTemplateStore = defineStore('templateStore', () => {
  /**公共作品 */
  const templatesList = ref<TemplateItem[]>([])
  const templateTotal = ref(0)
  const setTemplateList = async (
    params?: IGetWorkTemplateListProps,
    reset = false
  ) => {
    const result = await getWorkTEmplateListAPI(params)
    const { data } = result.data
    templatesList.value = reset
      ? data.res
      : [...templatesList.value, ...data.res]
    templateTotal.value = data.count
  }
  const resetTemplateList = () => {
    templatesList.value = []
    templateTotal.value = 0
  }
  /**个人作品 */
  const myWorkTemplate = ref<IWorkTemplateList[]>([])
  const myWorkTemplateTotal = ref(0)
  const setMyWorkTemplate = async (params: IGetWorkTemplateListProps) => {
    const result = await getPersonalWorkListAPI(params)
    const { data } = result.data
    myWorkTemplate.value = data.res
    myWorkTemplateTotal.value = data.count
  }
  const resetMyWorkTemplate = () => {
    myWorkTemplate.value = []
    myWorkTemplateTotal.value = 0
  }
  return {
    templatesList,
    setTemplateList,
    templateTotal,
    resetTemplateList,
    myWorkTemplate,
    setMyWorkTemplate,
    myWorkTemplateTotal,
    resetMyWorkTemplate,
  }
})

export default useTemplateStore
